package com.asion.vmovie.ui.MVPModel;

import com.asion.vmovie.api.ApiService;
import com.asion.vmovie.beans.DetialsBean;
import com.asion.vmovie.ui.MVPContract.SearchContract;
import com.asion.vmovie.utils.retrofitCache.SetCacheUtils;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by asion on 16-10-20.
 */

public class SearchModel implements SearchContract.Model {
    @Override
    public void getSearchDetials(int p, int size, String kw, SearchContract.OnGetSearchData onGetSearchData) {

        Observable<DetialsBean> searchData = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(SetCacheUtils.setCache())
                .baseUrl(ApiService.BASE_URL)
                .build()
                .create(ApiService.class)
                .getSearchData(p, size, kw);
        searchData.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(detialsBean -> onGetSearchData.onGetSuccess(detialsBean)
                ,throwable -> onGetSearchData.onGetError(throwable));
    }
}
